/**  
 * Copyright (c) 2010 University of Pennsylvania.
 *     All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an "AS
 *  IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 *  express or implied.  See the License for the specific language
 *  governing permissions and limitations under the License.
 *
 */
 
#ifdef MINIMUM_MEMORY
  #define HEAPMAN_BUFFERSIZE  1500
#else
  #define HEAPMAN_BUFFERSIZE  20000
#endif

#define HEAPMAN_BLOCKSIZE   4
#define HEAPMAN_BLOCKCOUNT  DIV_CEIL(HEAPMAN_BUFFERSIZE, HEAPMAN_BLOCKSIZE)

#define USE_HEAPMAN           // if not defined, will use standard heap manager
//#define DEBUG_MEM             // if defined, display info on allocation/deallocation 
//#define DISABLE_FREE          // if defined, do not free any memory

#ifdef DEBUG_MEM
#define mmalloc(s)      __mmalloc(s, __FUNCTION__, __LINE__)
#define mcalloc(s, b)   __mcalloc(s, b, __FUNCTION__, __LINE__)
#define mrealloc(p, s)  __mrealloc(p, s, __FUNCTION__, __LINE__)
#define mfree(p)        __mfree(p, __FUNCTION__, __LINE__)
#else
#define mmalloc(s)      __mmalloc(s)
#define mcalloc(s, b)   __mcalloc(s, b)
#define mrealloc(p, s)  __mrealloc(p, s)
#define mfree(p)        __mfree(p)
#endif

uint16_t heapman_firstfreeblock, heapman_usage, heapman_maxusage;
uint8_t heapman_buffer[HEAPMAN_BUFFERSIZE];
uint8_t heapman_used[DIV_CEIL(HEAPMAN_BLOCKCOUNT, 8)];
uint8_t heapman_blockend[DIV_CEIL(HEAPMAN_BLOCKCOUNT, 8)];

void init_mheapman();

#ifdef DEBUG_MEM
void *__mmalloc(uint16_t size, const char *fn_name, int line);
void *__mcalloc(uint16_t size, uint16_t block_count, const char *fn_name, int line);
void *__mrealloc(void *p, uint16_t size, const char *fn_name, int line);
void __mfree(void *p, const char *fn_name, int line);
#else
void *__mmalloc(uint16_t size);
void *__mcalloc(uint16_t size, uint16_t block_count);
void *__mrealloc(void *p, uint16_t size);
void __mfree(void *p);
#endif

